package com.lap.analysis.file.methods;

import com.lap.analysis.source.entity.MethodInfo;

import static com.lap.analysis.tools.Constants.*;

/**
 * 方法接口
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
public interface AbstractMethod {

  String EXPECT_JSON = ".andExpect(jsonPath(\"$.code\").value(%s));// just";

  String CODE_JUST = "resultCode";

  String MAP_CODE_JUST = "map.getInteger(\"resultCode\")";

  String EXPECT_STATUS_IS_OK = ".andExpect(status().isOk()).andDo(print())// status";

  String PERFORM = "mockMvc.perform(%s(\"%s\")";

  String PERFORM_PRE = "mockMvc.perform(%s(\"%s\"";

  String CREATE_MAP = "%sMap<String,Object> _paraMap = new HashMap<>(%s);%s";

  String PUT_MAP = "%s_paraMap.put(\"%s\",%s);%s";

  String CONTENT = "String _content = JsonUtil.toJson(_paraMap);";

  String CONTENT_TYPE = ".contentType(MediaType.APPLICATION_JSON))// api";

  String execute(MethodInfo methodInfo);

  default String formatter(String pattern, Object... object) {
    return String.format(pattern, object);
  }

  default String getCode(MethodInfo methodInfo) {
    return methodInfo.isArgMap() ? MAP_CODE_JUST : CODE_JUST;
  }

  // @formatter on
  default String getParamMap(MethodInfo methodInfo) {
    StringBuilder result = new StringBuilder();

    result.append(formatter(CREATE_MAP, TAB2, methodInfo.getFields().size(), WRAP));
    methodInfo
        .getFields()
        .forEach(
            item -> {
              result.append(formatter(PUT_MAP, TAB2, item.getDataName(), item.getDataName(), WRAP));
            });

    result.append(TAB2).append(CONTENT).append(WRAP2);
    return result.toString();
  }
  // @formatter off

}
